﻿using System;
using System.Web;
using FX.CC.Andon.Business.ReportManage;
using FX.CC.Andon.Entity.ReportManage;
using FX.CC.Andon.Utility.Common;

namespace FX.CC.Andon.WebSite.Service
{
    /// <summary>
    /// ReportManageHandler 的摘要说明
    /// </summary>
    public class ReportManageHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                var action = DtRequest.GetQueryString("action");
                if (string.IsNullOrEmpty(action)) return;
                var method = this.GetType().GetMethod(action);
                if (method == null) return;
                object[] parameters = { context };
                method.Invoke(this, parameters);
            }
            catch (Exception ex)
            {
                EventsLog.WriteLog("ErrorInfo" + ex.Message);
            }

        }

        #region 异常情况报表
        public void GetAbnormalReptList(HttpContext context)
        {
            var bll = new AbnormalReptBusiness();
            var qcmodel = new AbnormalQcEntity
            {
                BizDateFrom = context.Request["BizDateFrom"],
                BizDateEnd = context.Request["BizDateEnd"],
                LineID = context.Request["LineID"],
                EqID = context.Request["EqID"],
                ETypeCode = context.Request["ETypeCode"],
                PrdID = context.Request["PrdID"],
                IsCancle = context.Request["IsCancle"],
                PageIndex = Convert.ToInt32(context.Request["pageIndex"]),
                PageSize = Convert.ToInt32(context.Request["pageSize"]),
                SortField = context.Request["sortField"],
                SortOrder = context.Request["sortOrder"]
            };
            var json = bll.GetAbnormalReptList(qcmodel);
            context.Response.Write(json);
            context.Response.End();
        }
        /// <summary>
        /// 明细
        /// </summary>
        /// <param name="context"></param>
        public void GetAbnormalDetailList(HttpContext context)
        {
            var bll = new AbnormalReptBusiness();
            var qcmodel = new AbnormalQcEntity
            {
                EventID = context.Request["EventID"],
                PageIndex = Convert.ToInt32(context.Request["pageIndex"]),
                PageSize = Convert.ToInt32(context.Request["pageSize"]),
                SortField = context.Request["sortField"],
                SortOrder = context.Request["sortOrder"]
            };
            var json = bll.GetAbnormalDetailList(qcmodel);
            context.Response.Write(json);
            context.Response.End();
        }

        /// <summary>
        /// 发信明细
        /// </summary>
        /// <param name="context"></param>
        public void GetSenderDetailList(HttpContext context)
        {
            var bll = new AbnormalReptBusiness();
            var qcmodel = new AbnormalQcEntity
            {
                EventID = context.Request["EventID"],
                PageIndex = Convert.ToInt32(context.Request["pageIndex"]),
                PageSize = Convert.ToInt32(context.Request["pageSize"]),
                SortField = context.Request["sortField"],
                SortOrder = context.Request["sortOrder"]
            };
            var json = bll.GetSenderDetailList(qcmodel);
            context.Response.Write(json);
            context.Response.End();
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="context"></param>
        public void ExportAbnormalRept(HttpContext context)
        {
            var bll = new AbnormalReptBusiness();
            var qcmodel = new AbnormalQcEntity
            {
                BizDateFrom = context.Request["BizDateFrom"],
                BizDateEnd = context.Request["BizDateEnd"],
                LineID = context.Request["LineID"],
                EqID = context.Request["EqID"],
                ETypeCode = context.Request["ETypeCode"],
                PrdID = context.Request["PrdID"],
                IsCancle = context.Request["IsCancle"]
            };
            var dt = bll.ExportAbnormalRept(qcmodel);
            if (dt.Rows.Count <= 0) return;
            const bool specificTime = true;
            dt.Columns[0].ColumnName = "作废标记";
            dt.Columns[1].ColumnName = "产品编号";
            dt.Columns[2].ColumnName = "产品名称";
            dt.Columns[3].ColumnName = "产线名称";
            dt.Columns[4].ColumnName = "设备编号";
            dt.Columns[5].ColumnName = "设备名称";
            dt.Columns[6].ColumnName = "异常触发时间";
            dt.Columns[7].ColumnName = "异常解除时间";
            dt.Columns[8].ColumnName = "持续时间（分钟）";
            dt.Columns[9].ColumnName = "异常原因";
            dt.Columns[10].ColumnName = "异常明细编号";
            dt.Columns[11].ColumnName = "异常明细名称";
            dt.Columns[12].ColumnName = "操作人";
            dt.Columns[13].ColumnName = "处理人员";
            dt.Columns[14].ColumnName = "备注";
            if (dt.Rows.Count > 0)
            {
                ExcelHelper.ExportByWeb(dt, "暗灯系统-异常情况报表", "暗灯系统-异常情况报表.xls", specificTime);
            }
        }
        #endregion

        #region 异常时长汇总图形报表
        public void GetExceptionSummaryList(HttpContext context)
        {
            var bll = new ExceptionSummaryBusiness();
            var qcmodel = new AbnormalQcEntity
            {
                BizDateFrom = context.Request["BizDateFrom"],
                BizDateEnd = context.Request["BizDateEnd"],
                LineID = context.Request["LineID"],
                EqID = context.Request["EqID"]
            };
            var json = bll.GetExceptionSummaryList(qcmodel);
            context.Response.Write(json);
            context.Response.End();
        }
        #endregion

        #region 异常柏拉图
        public void GetExceptionPlatoList(HttpContext context)
        {
            var bll = new ExceptionPlatoBusiness();
            var qcmodel = new AbnormalQcEntity
            {
                BizDateFrom = context.Request["BizDateFrom"],
                BizDateEnd = context.Request["BizDateEnd"],
                LineID = context.Request["LineID"],
                EqID = context.Request["EqID"]
            };
            var json = bll.GetExceptionPlatoList(qcmodel);
            context.Response.Write(json);
            context.Response.End();
        }
        #endregion

        #region 换模
        public void GetModelRecordReportList(HttpContext context)
        {
            var bll = new ModelRecBusiness();
            var qcmodel = new ModelRecQcEntity
            {
                BizDateFrom = context.Request["BizDateFrom"],
                BizDateEnd = context.Request["BizDateEnd"],
                PrdID = context.Request["PrdID"],
                FlowID = context.Request["FlowID"],
                PageIndex = Convert.ToInt32(context.Request["pageIndex"]),
                PageSize = Convert.ToInt32(context.Request["pageSize"]),
                SortField = context.Request["sortField"],
                SortOrder = context.Request["sortOrder"]
            };
            var json = bll.GetModelRecordReportList(qcmodel);
            context.Response.Write(json);
            context.Response.End();
        }
        public void GetModelAvgReportList(HttpContext context)
        {
            var bll = new ModelRecBusiness();
            var qcmodel = new ModelRecQcEntity
            {
                BizDateFrom = context.Request["BizDateFrom"],
                BizDateEnd = context.Request["BizDateEnd"],
                PrdID = context.Request["PrdID"],
                FlowID = context.Request["FlowID"],
                PageIndex = Convert.ToInt32(context.Request["pageIndex"]),
                PageSize = Convert.ToInt32(context.Request["pageSize"]),
                SortField = context.Request["sortField"],
                SortOrder = context.Request["sortOrder"]
            };
            var json = bll.GetModelAvgReportList(qcmodel);
            context.Response.Write(json);
            context.Response.End();
        }
        #endregion

        #region 生产达成率
        public void GetAchievingRateReptList(HttpContext context)
        {
            var bll = new AchievingRateBusiness();
            var qcmodel = new AchievingRateQcEntity
            {
                BizDateFrom = context.Request["BizDateFrom"],
                BizDateEnd = context.Request["BizDateEnd"],
                LineID = context.Request["LineID"],
                EqID = context.Request["EqID"],
                ShiftID = context.Request["ShiftID"],
                PrdID = context.Request["PrdID"],
                PageIndex = Convert.ToInt32(context.Request["pageIndex"]),
                PageSize = Convert.ToInt32(context.Request["pageSize"]),
                SortField = context.Request["sortField"],
                SortOrder = context.Request["sortOrder"]
            };
            var json = bll.GetAchievingRateReptList(qcmodel);
            context.Response.Write(json);
            context.Response.End();
        }
        public void ExportAchievingRateRept(HttpContext context)
        {
            var bll = new AchievingRateBusiness();
            var qcmodel = new AchievingRateQcEntity
            {
                BizDateFrom = context.Request["BizDateFrom"],
                BizDateEnd = context.Request["BizDateEnd"],
                LineID = context.Request["LineID"],
                EqID = context.Request["EqID"],
                ShiftID = context.Request["ShiftID"],
                PrdID = context.Request["PrdID"],
            };
            var dt = bll.ExportAchievingRateRept(qcmodel);
            if (dt.Rows.Count <= 0) return;
            const bool specificTime = true;
            dt.Columns[0].ColumnName = "日期";
            dt.Columns[1].ColumnName = "产品编号";
            dt.Columns[2].ColumnName = "产品名称";
            dt.Columns[3].ColumnName = "设备编号";
            dt.Columns[4].ColumnName = "设备名称";
            dt.Columns[5].ColumnName = "产线编号";
            dt.Columns[6].ColumnName = "产线名称";
            dt.Columns[7].ColumnName = "班次";
            dt.Columns[8].ColumnName = "计划产量";
            dt.Columns[9].ColumnName = "实际产量";
            dt.Columns[10].ColumnName = "计划开始时间";
            dt.Columns[11].ColumnName = "计划结束时间";
            dt.Columns[12].ColumnName = "生产时长(分钟)";
            dt.Columns[13].ColumnName = "达成率";
            if (dt.Rows.Count > 0)
            {
                ExcelHelper.ExportByWeb(dt, "暗灯系统-达成率报表", "暗灯系统-达成率报表.xls", specificTime);
            }
        }

        #endregion

        #region 异常大类汇总报表
        public void GetEventTimeList(HttpContext context)
        {
            var bll = new EventSummaryReptBusiness();
            var qcmodel = new EventSummaryReptQcEntity
            {
                BizDateFrom = context.Request["BizDateFrom"],
                BizDateEnd = context.Request["BizDateEnd"],
                LineID = context.Request["LineID"],
                EqID = context.Request["EqID"],
                ETypeCode = context.Request["ETypeCode"],
            };
            var json = bll.GetEventTimeList(qcmodel);
            context.Response.Write(json);
            context.Response.End();
        }
        public void GetEventNumberList(HttpContext context)
        {
            var bll = new EventSummaryReptBusiness();
            var qcmodel = new EventSummaryReptQcEntity
            {
                BizDateFrom = context.Request["BizDateFrom"],
                BizDateEnd = context.Request["BizDateEnd"],
                LineID = context.Request["LineID"],
                EqID = context.Request["EqID"],
                ETypeCode = context.Request["ETypeCode"],
            };
            var json = bll.GetEventNumberList(qcmodel);
            context.Response.Write(json);
            context.Response.End();
        }

        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}